package pers.cj.pdos.controller.other;

import com.alibaba.excel.ExcelWriter;
import com.alibaba.excel.metadata.Sheet;
import com.alibaba.excel.support.ExcelTypeEnum;
import org.apache.ibatis.annotations.Param;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import pers.cj.pdos.controller.base.BaseController;
import pers.cj.pdos.controller.entity.ResponseData;
import pers.cj.pdos.controller.entity.ResponseEntity;
import pers.cj.pdos.controller.entity.ResponseList;
import pers.cj.pdos.entity.Attr;
import pers.cj.pdos.entity.BusinessSet;
import pers.cj.pdos.entity.Company;
import pers.cj.pdos.entity.Sale;
import pers.cj.pdos.excel.ExcelHouseQuery;
import pers.cj.pdos.excel.ExcelHouseQueryDetail;
import pers.cj.pdos.service.IGoodsService;
import pers.cj.pdos.service.IGoodsStockService;
import pers.cj.pdos.service.ISaleService;
import pers.cj.pdos.task.CleanTempExcelTask;
import pers.cj.pdos.utils.DateUtils;
import pers.cj.pdos.utils.FileProperUtil;
import pers.cj.pdos.utils.PdosUtils;
import pers.cj.pdos.utils.Utils;
import pers.cj.pdos.vo.*;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 库存查询
 */
@Controller
@RequestMapping("/funds")
public class SaleTicketController extends BaseController {
    @Autowired
    ISaleService saleService;

    @RequiresPermissions("saleTicket:query")
    @RequestMapping("/saleTicket")
    public String saleTicket(){
        return "funds/saleTicket";
    }

    @ResponseBody
    @RequiresPermissions("saleTicket:query")
    @RequestMapping("/saleTicket/getList")
    public ResponseList getList(int page,
                                int limit,
                                String ticket,
                                String search,
                                String no,
                                String goodsName,
                                String remark,
                                String storeId,
                                String houseId,
                                String settAccountId,
                                String custom,
                                String deliveryDate1,
                                String deliveryDate2,
                                String makeTime1,
                                String makeTime2,
                                String userId,
                                String date1,
                                String date2,
                                String makeUserId){
        Company company = getCompany();

        UserVo userVo = getLoginUserVo();
        List<SaleVo> saleOrderVoList = saleService.listVoForTicket(company.getId(),
                page,
                limit,
                Utils.genInteger(ticket),
                Utils.genLikeData(search),
                Utils.genLikeData(no),
                Utils.genLikeData(goodsName),
                Utils.genLikeData(remark),
                storeId,
                houseId,
                settAccountId,
                Utils.genLikeData(custom),
                Utils.genDate(deliveryDate1),
                Utils.genDate(deliveryDate2),
                Utils.genDate(makeTime1),
                Utils.genDate(makeTime2),
                PdosUtils.getViewOrderStoreId(userVo, null),
                PdosUtils.getViewOrderUserId(userVo, userId),
                Utils.genDate(date1),
                Utils.genDate(date2),
                makeUserId);
        long count = saleService.countForTicket(company.getId(),
                Utils.genInteger(ticket),
                Utils.genLikeData(search),
                Utils.genLikeData(no),
                Utils.genLikeData(goodsName),
                Utils.genLikeData(remark),
                storeId,
                houseId,
                settAccountId,
                Utils.genLikeData(custom),
                Utils.genDate(deliveryDate1),
                Utils.genDate(deliveryDate2),
                Utils.genDate(makeTime1),
                Utils.genDate(makeTime2),
                PdosUtils.getViewOrderStoreId(userVo, null),
                PdosUtils.getViewOrderUserId(userVo, userId),
                Utils.genDate(date1),
                Utils.genDate(date2),
                makeUserId);
        return ResponseList.success(count,saleOrderVoList);
    }

    @ResponseBody
    @RequiresPermissions("saleTicket:ticket")
    @RequestMapping("/saleTicket/ticket")
    public ResponseEntity ticket(@RequestBody Sale sale){
        Sale db = saleService.getById(sale.getId());
        if (db.getTicket() == 3) {
            return ResponseEntity.fail("已开票");
        }
        Sale temp = new Sale();
        temp.setId(sale.getId());
        temp.setTicket(3);
        saleService.updateById(temp);

        return ResponseEntity.success();
    }

    @ResponseBody
    @RequiresPermissions("saleTicket:ticket")
    @RequestMapping("/saleTicket/backTicket")
    public ResponseEntity backTicket(@RequestBody Sale sale){

        Sale db = saleService.getById(sale.getId());
        if (db.getTicket() == 2) {
            return ResponseEntity.fail("已撤销开票");
        }
        Sale temp = new Sale();
        temp.setId(sale.getId());
        temp.setTicket(2);
        saleService.updateById(temp);
        return ResponseEntity.success();
    }

    @ResponseBody
    @RequiresPermissions("saleTicket:ticket")
    @RequestMapping("/saleTicket/batchTicket")
    public ResponseEntity batchTicket(@RequestBody List<String> ids){
        if (saleService.checkTicket(ids)) {
            return ResponseEntity.fail("您选择的单据中有已开票的单据，请重新选择。");
        }

        saleService.batchTicket(ids);
        return ResponseEntity.success();
    }
}
